// 直接导入模拟数据库
const mockDatabase = require('./backend/src/config/mockDatabase');
const { logger } = require('./backend/src/utils/logger');

async function debugExistingItems() {
  try {
    console.log('=== 检查现有菜单项的详细信息 ===\n');
    
    // 查询所有现有菜单项的详细信息
    const items = await mockDatabase.query('SELECT id, name, name_en, normalized_name, description, main_ingredients, category_id FROM menu_items WHERE deleted_at IS NULL');
    
    console.log(`找到 ${items.length} 个现有菜单项:`);
    items.forEach((item, index) => {
      console.log(`\n${index + 1}. 菜单项 ID: ${item.id}`);
      console.log(`   中文名: ${item.name}`);
      console.log(`   英文名: ${item.name_en}`);
      console.log(`   规范化名称: ${item.normalized_name}`);
      console.log(`   描述: ${item.description}`);
      console.log(`   主要配料: ${item.main_ingredients}`);
      console.log(`   分类ID: ${item.category_id}`);
    });
    
    console.log('\n=== 测试匹配逻辑 ===\n');
    
    // 测试我们的测试数据是否会匹配现有记录
    const testItems = [
      { name_en: 'Test Burger 1', normalized_name: 'test-burger-001' },
      { name_en: 'Test Drink 1', normalized_name: 'test-drink-001' }
    ];
    
    for (const testItem of testItems) {
      console.log(`测试项: ${testItem.name_en} (normalized: ${testItem.normalized_name})`);
      
      // 模拟V2导入系统的查询逻辑
      const matches = await mockDatabase.query(
        'SELECT id, name, name_en, normalized_name FROM menu_items WHERE (normalized_name = ? OR name_en = ?) AND deleted_at IS NULL LIMIT 1',
        [testItem.normalized_name, testItem.name_en]
      );
      
      if (matches.length > 0) {
        console.log(`  ✅ 匹配到现有记录:`);
        console.log(`     ID: ${matches[0].id}`);
        console.log(`     名称: ${matches[0].name} (${matches[0].name_en})`);
        console.log(`     规范化名称: ${matches[0].normalized_name}`);
      } else {
        console.log(`  ❌ 没有匹配到现有记录`);
      }
      console.log('');
    }
    
  } catch (error) {
    console.error('调试失败:', error);
  }
}

debugExistingItems();